package com.bergerkiller.bukkit.tc.statements;

import com.bergerkiller.bukkit.tc.Direction;
import com.bergerkiller.bukkit.tc.controller.MinecartGroup;
import com.bergerkiller.bukkit.tc.controller.MinecartMember;
import com.bergerkiller.bukkit.tc.events.SignActionEvent;
import org.bukkit.block.BlockFace;

public class StatementRedstone extends Statement {

    @Override
    public boolean match(String text) {
        return text.equals("redstone") || text.equals("powered");
    }

    @Override
    public boolean matchArray(String text) {
        return text.equals("rs") || text.equals("redstone") || text.equals("power") || text.equals("powered");
    }

    @Override
    public boolean requiresTrain() {
        return false;
    }

    @Override
    public boolean handle(MinecartMember<?> member, String text, SignActionEvent event) {
        return this.handle(text, event);
    }

    @Override
    public boolean handle(MinecartGroup group, String text, SignActionEvent event) {
        return this.handle(text, event);
    }

    @Override
    public boolean handleArray(MinecartMember<?> member, String[] names, SignActionEvent event) {
        return this.handle(names, event);
    }

    @Override
    public boolean handleArray(MinecartGroup group, String[] names, SignActionEvent event) {
        return this.handle(names, event);
    }

    public boolean handle(String text, SignActionEvent event) {
        return event.isPoweredRaw(false);
    }

    public boolean handle(String[] names, SignActionEvent event) {
        for (String name : names) {
            BlockFace direction = Direction.parse(name).getDirection(event.getFacing());
            if (event.getPower(direction).hasPower()) {
                return true;
            }
        }
        return false;
    }

    @Override
    public boolean requiredEvent() {
        return true;
    }
}
